package com.smile.chatview;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicTextFieldUI;
import javax.swing.text.JTextComponent;

public class SearchTextField extends BasicTextFieldUI {

	private JComponent jc;
	private BufferedImage image;
	private boolean isFouseGained = false;

	public SearchTextField() {
		super();
		try {
			image = ImageIO.read(new File("resources/img/search.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void installUI(JComponent c) {
		super.installUI(c);
		this.jc = c;

		JTextComponent text = (JTextComponent) c;
		text.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
	}

	@Override
	protected void paintBackground(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		if(isFouseGained){
			g2d.setColor(new Color(166, 180, 193));
		}else{
			g2d.setColor(new Color(48, 57, 66));
		}
		g2d.fillRoundRect(0, 0, jc.getWidth(), jc.getHeight(), 8, 8);
		if (image != null) {
			g2d.drawImage(image, 0, 3, jc.getHeight() - 5, jc.getHeight() - 5, null);
		}
	}
	
	public void isFocusGained(boolean is){
		isFouseGained = is;
		
	}

}
